package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.util.JWTUtil;
import com.example.demo.util.Result;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("api/login")
public class LoginController {

    @Resource
    private UserService userService;

    @PostMapping
    public Result login(@RequestBody @Validated User user) {
        User u = userService.login(user);
        if (null == u) {
            return Result.error("用户名或密码错误");
        }
        return Result.success(JWTUtil.getToken(u));
    }

}
